package com.hd.pzhwlxt.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hd.pzhwlxt.pojo.Material;
import com.hd.pzhwlxt.service.MaterialService;
import com.hd.pzhwlxt.service.MaterialpriceService;
import com.hd.pzhwlxt.utils.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @create: 2022-09-21 20:18
 * @author: 怀念天空
 * @Description: 材料信息，获取所有基本材料，多表查价格信息
 */
@RestController
@RequestMapping("/materialInfo")
public class MaterialInfoController {

    @Autowired
    private MaterialService materialService;

    @Autowired
    private MaterialpriceService materialpriceService;

    @GetMapping("/get")
    public Response<Object> getAllMaterialInfo(@RequestParam(value = "pageNum",required = false) Integer pageNum, @RequestParam(value = "pageSize",required = false) Integer pageSize){
        if (pageNum == null || pageSize == null){
            List<Material> result = materialService.getAllMaterialInfo();
            if(result!=null){
                return Response.createSuccessResponse("查询成功", result);
            }
        }else {
            Page<Material> result = materialService.getAllMateriaAndPricePage(pageNum, pageSize);
            if(result!=null){
                return Response.createSuccessResponse("查询成功", result);
            }
        }
        return Response.createErrorResponse("查询失败");
    }


    @GetMapping("/get/{id}")
    public Response<Material> getMaterialInfoById(@PathVariable Integer id){
        Material result = materialService.getMaterialById(id);
        if(result!=null){
            return Response.createSuccessResponse("查询成功", result);
        }
        return Response.createErrorResponse("查询失败");
    }

    /**
     * 获取的信息是两个类的
     * @param material
     * @return
     */
    @PostMapping("/add")
    public Response<String> addMaterialInfo(@RequestBody Material material){
        boolean save = materialService.addMaterial(material);
        if (save){
            return Response.createSuccessResponse("添加成功");
        }
        return Response.createErrorResponse("添加失败");
    }

    @DeleteMapping("/delmore/{ids}")
    public Response<String> deleteMaterialInfoByIds(@PathVariable Integer[] ids){
        boolean remove = materialService.deleteByIds(Arrays.asList(ids));
        if (remove){
            return Response.createSuccessResponse("删除成功");
        }
        return Response.createErrorResponse("删除失败");
    }

    @PutMapping("/update")
    public Response<String> updateStaffInfo(@RequestBody Material material){

        boolean update = materialService.updatematerial(material);
        
        if (update){
            return Response.createSuccessResponse("修改成功");
        }
        return Response.createErrorResponse("修改失败");
    }

}
